<?php


namespace App\Http\Controllers\Api;
use App\Http\Controllers\BaseController;
use App\Models\Article;
use App\Models\Comment;
use App\Models\Report;
use Illuminate\Http\Request;

class ReportController extends BaseController
{

    public function store(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
            'enum_id' => 'required',
            'type' => 'required|in:1,2'
        ]);
        $user_id = $this->getUserId();
        $input = $request->input();
        if (in_array($input['type'], [1])) {
            $article = Article::find($input['id']);
            if (!$article)
                return $this->failed('不存在');

            Report::create([
                'user_id' => $user_id,
                'entity_id' => $article->id,
                'type' => $article->type,
                'publish_user_id' => $article->user_id,
                'enum_id' => $input['enum_id'],
            ]);

        } else {
            $comment = Comment::find($input['id']);
            if (!$comment)
                return $this->failed('不存在');

            Report::create([
                'user_id' => $user_id,
                'entity_id' => $comment->entity_id,
                'type' => 4,
                'publish_user_id' => $comment->user_id,
                'enum_id' => $input['enum_id'],
                'comment_msg' => $comment->message,
                'comment_id' => $comment->id
            ]);
        }

        return $this->success([]);
    }

}

